﻿using System.Collections.Generic;
using System.Web.Mvc;

namespace ValidationFramework.Web.MVC
{
	public static class ModelStateBinder
	{
		public static void UpdateModelState(object model, ModelStateDictionary modelState)
		{
			if (model == null)
				return;

			var results = Validation.Validate(model);
			// skip if there are no validation errors
			if (results.Count <= 0)
			{
			    return;
			}

			foreach (var error in results)
			{
				var key = error.MemberName;
				modelState.AddModelError(key, error.ErrorMessage);
			}
		}

		public static void UpdateModelState(List<ValidationError> errors, ModelStateDictionary modelState)
		{
			// skip if there are no validation errors
			if (errors.Count <= 0)
			{
				return;
			}

			foreach (var error in errors)
			{
				var key = error.MemberName;
				modelState.AddModelError(key, error.ErrorMessage);
			}
		}
	}
}
